package com.ethink.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ethink.order.api.constant.OrderConstant;
import com.ethink.order.api.info.TobSaleSettlementDetailOrderInfo;
import com.ethink.order.api.request.TobSaleSettlementDetailOrderAddRequest;
import com.ethink.order.api.request.TocSaleImportJobLogAddRequest;
import com.ethink.order.common.entity.TobSaleSettlementDetailOrder;
import com.ethink.order.common.mapper.TobSaleSettlementDetailOrderMapper;
import com.ethink.order.service.TobSaleSettlementDetailOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * TOB销售结算详单 Service接口实现
 *
 * @author default
 * @date 2022-11-17
 */
@Slf4j
@Service
public class TobSaleSettlementDetailOrderServiceImpl implements TobSaleSettlementDetailOrderService {

    @Autowired
    private TobSaleSettlementDetailOrderMapper tobSaleSettlementDetailOrderMapper;

    @Override
    public List<TobSaleSettlementDetailOrderInfo> queryDetailOrderByPo(String poOrderNo) {
        if (StrUtil.isBlank(poOrderNo)) {
            return CollUtil.newArrayList();
        }
        return tobSaleSettlementDetailOrderMapper.queryDetailOrderByPo(poOrderNo);
    }

    @Override
    public void batchInsert(List<TobSaleSettlementDetailOrderAddRequest> item, String orderNo, String saleChannel) {
        //如果是唯品 po+mateSku
        Map<String, List<TobSaleSettlementDetailOrderAddRequest>> groupRequest;
        if (OrderConstant.Z102.equals(saleChannel)) {
            groupRequest = item.stream().collect(Collectors.groupingBy(a -> a.getPoOrderNo() + "-" + a.getMateSku()));
        } else {
            groupRequest = item.stream().collect(Collectors.groupingBy(TobSaleSettlementDetailOrderAddRequest::getMateSku));
        }
        List<TobSaleSettlementDetailOrderAddRequest> groupItem = new ArrayList<>();
        for (Map.Entry<String, List<TobSaleSettlementDetailOrderAddRequest>> listEntry : groupRequest.entrySet()) {
            TobSaleSettlementDetailOrderAddRequest addRequest = new TobSaleSettlementDetailOrderAddRequest();
            List<TobSaleSettlementDetailOrderAddRequest> value = listEntry.getValue();
            TobSaleSettlementDetailOrderAddRequest settlementRequest = value.get(0);
            addRequest.setMateSku(settlementRequest.getMateSku());
            addRequest.setMateSkuName(settlementRequest.getMateSkuName());
            addRequest.setPoOrderNo(settlementRequest.getPoOrderNo());
            int settleQty = 0;
            BigDecimal totalPrice = BigDecimal.ZERO;
            for (TobSaleSettlementDetailOrderAddRequest request : value) {
                settleQty += request.getSettleQty();
                totalPrice = totalPrice.add(request.getSettlePrice().multiply(new BigDecimal(request.getSettleQty())));
            }
            addRequest.setSettleQty(settleQty);
            addRequest.setSettlePrice(settleQty != 0 ? totalPrice.divide(new BigDecimal(settleQty), 6, BigDecimal.ROUND_DOWN) : BigDecimal.ZERO);
            groupItem.add(addRequest);
        }
        for (TobSaleSettlementDetailOrderAddRequest settlementDetailOrderAddRequest : groupItem) {
            addTobSaleSettlementDetailOrder(settlementDetailOrderAddRequest, orderNo);
        }
    }

    @Override
    public void addTobSaleSettlementDetailOrder(TobSaleSettlementDetailOrderAddRequest request, String orderNo) {
        TobSaleSettlementDetailOrder saleSettlementDetailOrder = new TobSaleSettlementDetailOrder();
        String detailOrderNo = IdUtil.getSnowflakeNextIdStr();
        BeanUtils.copyProperties(request, saleSettlementDetailOrder);
        saleSettlementDetailOrder.setOrderNo(orderNo);
        saleSettlementDetailOrder.setDetailOrderNo(detailOrderNo);
        tobSaleSettlementDetailOrderMapper.insert(saleSettlementDetailOrder);
    }
}
